<?php
/*
	我的下线
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
if($_RQ['op']=='main'){
	if(!$this->setting['level']){
		exi('未开启分销！','error','jy_weishop/index/');
	}
	$return['setting']=$this->plugin_setting('page_setting');
	$levels=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title','level'),'','');
	if($_RQ['istui']){
		if($_RQ['istui']){
			$return['allagentnum']=$return['mytuinum'];
		}
		$levels[]=array('id'=>0,'title'=>'默认等级');
		foreach($levels as &$lv){
			$lv['num']=$this->count('member',array('level_id'=>$lv['id'],'agent_id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			if($_RQ['islist'] && $lv['id']==$_RQ['level_id']){
				$return['allagentnum']=$lv['num'];
			}
		}
	}else{
		$getteamids=$this->model('member')->getteamids(intval($_RQ['ismid']),12,true,$this->setting['down_agent']);
		$return['allagentnum']=count($getteamids);
		$getteamids=$getteamids?$getteamids:-1;
		$levels[]=array('id'=>0,'title'=>'默认等级');
		foreach($levels as &$lv){
			$lv['num']=$this->count('member',array('level_id'=>$lv['id'],'mid'=>$getteamids,'uniacid'=>$_SESSION['uniacid']));
			if($_RQ['islist'] && $lv['id']==$_RQ['level_id']){
				$return['allagentnum']=$lv['num'];
			}
		}
	}
	if($this->setting['down_nolevel']){
		$_RQ['islist']=1;
	}
	$return['levels']=$levels;
	$mid=$_RQ['ismid']?:$_SESSION['uid'];
	$return['mytuinum']=$this->count('member',array('agent_id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$return['member']=cfc('core')->member_show($mid);
	$return['params']=array('level'=>1,'page'=>1,'ismid'=>intval($_RQ['ismid']),'islist'=>intval($_RQ['islist']),'istui'=>intval($_RQ['istui']));
	$return['params']['level_id']=intval($_RQ['level_id']);
	$return['psize']=20;
	$return['list']=array();
	set_html('title',$return['setting']['tmydown']?:'我的下线');
	exi($return);
}
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$params['level_id']=intval($_RQ['level_id']);
	if(!$params['level_id']){
		if($this->setting['down_nolevel']){
			unset($params['level_id']);
		}
		if($this->setting['down_agent']){
			$params['is_agent']=1;
			$params['level_id >']=0;
		}
	}
	$ismid=intval($_RQ['ismid']);
	if($ismid){
		$agent_ids=$ismid;
	}elseif($this->setting['down_nolevel']!=1 || $_RQ['level']==3){
		$agent_ids=$this->model('member')->getteamids('',12,0,$this->setting['down_agent']);
	}
	if($this->setting['down_nolevel'] && $_RQ['level']==1){
		$agent_ids=$_SESSION['uid'];
	}elseif($this->setting['down_nolevel'] && $_RQ['level']==2){
		$agent_ids=$this->getall('member',array('uniacid'=>$_SESSION['uniacid'],'agent_id'=>$_SESSION['uid']),array('mid'),'mid');
		$agent_ids=$agent_ids?array_keys($agent_ids):-1;
	}
	if($_RQ['istui']){
		$agent_ids=$_SESSION['uid'];
	}
	if(!$agent_ids){
		exi('none');
	}
	$params['agent_id']=$agent_ids;
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'mid desc',
	);
	$list=$this->get_list('member',$parameter);
	if(!$list){
		exi('none');
	}
	foreach($list as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		if(!$item['member']['id']){
			pdo_delete('jy_commission_member',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));	
		}
		$item['agenttime']=date('Y-m-d H:i:s',$item['agenttime']);
		$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
		$item['member_num']=$this->count('member',array('agent_id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('mid'),'mid');
		$item['leveltitle']=$this->getcolumn('level',array('id'=>$item['level_id']),'title');
		$item['leveltitle']=$item['leveltitle']?:'默认等级';
	}
	exi($list);
}
?>